package com.longagoewk.springboot.dao;

import com.longagoewk.springboot.LogConfig;
import com.longagoewk.springboot.entity.ExplainBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ExplainDao {

    //JDBC访问数据模板
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //获取讲解实体
    public ExplainBean getExplain(ExplainBean explainBean)
    {
        String sql = "SELECT * FROM explaintable where eID=?";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(ExplainBean.class),explainBean.geteID());
        }catch (EmptyResultDataAccessException e)
        {
            LogConfig.warning("数据库 - 未存在该讲解信息:"+explainBean.geteID());
            return null;
        }
    }
    //获取展厅讲解列表
    public List<ExplainBean> getExplainList(ExplainBean explainBean) {
        String sql = "SELECT * FROM explaintable where hname=? or mID=?";
        try {
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper(ExplainBean.class),explainBean.getHname(),explainBean.getmID());
        }catch (EmptyResultDataAccessException e)
        {
            LogConfig.error("数据库 - 获取讲解列表失败");
            return null;
        }

    }

    //删除讲解
    public boolean deleteExplain(ExplainBean explainBean)
    {
        String sql="delete from explaintable where eID=?";
        try {
            int result= jdbcTemplate.update(sql,explainBean.geteID());
            return result>0;
        } catch (DataAccessException e) {
            LogConfig.error("数据库 - 删除讲解信息"+explainBean.geteID()+"失败");
            return false;
        }
    }

    //更新讲解信息
    public boolean updataExplain(ExplainBean explainBean){
        String sql = "UPDATE explaintable SET mID = ?, hname = ?, ename = ?, eimage = ?, evoice = ? WHERE eID = ?";
        try {
            int result = jdbcTemplate.update(sql,explainBean.getmID(),explainBean.getHname(),explainBean.getEname(),explainBean.getEimage(),explainBean.getEvoice(),explainBean.geteID());
            return result > 0;
        } catch (Exception e) {
            LogConfig.error("数据库 - 讲解"+explainBean.geteID()+"更新信息失败");
            return false;
        }
    }

    //添加讲解
    public boolean addExplain(ExplainBean explainBean) {
        String sql="INSERT INTO explaintable(eID, mID, hname, ename, eimage, evoice) VALUES(?, ?, ?, ?, ?, ?)";
        try {
            return jdbcTemplate.update(sql, explainBean.geteID(), explainBean.getmID(),explainBean.getHname(),explainBean.getEname(),explainBean.getEimage(),explainBean.getEvoice())>0;
        } catch (DataAccessException e) {
            LogConfig.error("数据库 - 商品"+explainBean.geteID()+"添加失败");
            return false;
        }
    }

}
